package com.wangchao.administrator.zenmechi.ui.fragment.homefragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.base.BaseFragment;
import com.wangchao.administrator.zenmechi.bean.homebean.GoodsHotListBean;
import com.wangchao.administrator.zenmechi.bean.homebean.Mainbean;
import com.wangchao.administrator.zenmechi.config.UrlConfig;
import com.wangchao.administrator.zenmechi.image.BannerLayout;
import com.wangchao.administrator.zenmechi.image.MyListView;
import com.wangchao.administrator.zenmechi.ui.detailactivity.DetailActivity;
import com.wangchao.administrator.zenmechi.ui.lacationactivity.LocationActiviy;
import com.wangchao.administrator.zenmechi.ui.specialactivity.SpecialActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/17.
 */
public class HomeFragment extends BaseFragment implements HomeContract.View {


    @BindView(R.id.tv_location_hone)
    TextView tvLocationHone;
    @BindView(R.id.iv_location_home)
    ImageView ivLocationHome;
    @BindView(R.id.iv_search_home)
    ImageView ivSearchHome;
    @BindView(R.id.vp_home)
    BannerLayout vpHome;
    @BindView(R.id.iv_activity_home)
    ImageView ivActivityHome;
    @BindView(R.id.tv_activity_home)
    TextView tvActivityHome;
    @BindView(R.id.rela_activity_home)
    RelativeLayout relaActivityHome;
    @BindView(R.id.iv_cook_home)
    ImageView ivCookHome;
    @BindView(R.id.tv_cook_home)
    TextView tvCookHome;
    @BindView(R.id.rela_cook_home)
    RelativeLayout relaCookHome;
    @BindView(R.id.iv_order_home)
    ImageView ivOrderHome;
    @BindView(R.id.tv_order_home)
    TextView tvOrderHome;
    @BindView(R.id.rela_order_home)
    RelativeLayout relaOrderHome;
    @BindView(R.id.iv_sign_home)
    ImageView ivSignHome;
    @BindView(R.id.tv_sign_home)
    TextView tvSignHome;
    @BindView(R.id.rela_sign_home)
    RelativeLayout relaSignHome;
    @BindView(R.id.ll_activity_home)
    LinearLayout llActivityHome;
    @BindView(R.id.icon_news_home)
    ImageView iconNewsHome;
    @BindView(R.id.tv_todayspec_home)
    TextView tvTodayspecHome;
    @BindView(R.id.tv_more_home)
    TextView tvMoreHome;
    @BindView(R.id.rela_specia_home)
    RelativeLayout relaSpeciaHome;
    @BindView(R.id.iv_onespc_home)
    ImageView ivOnespcHome;
    @BindView(R.id.iv_twospc_home)
    ImageView ivTwospcHome;
    @BindView(R.id.iv_thiredspc_home)
    ImageView ivThiredspcHome;
    @BindView(R.id.ll_inner_spec_home)
    LinearLayout llInnerSpecHome;
    @BindView(R.id.ll_specitem_home)
    LinearLayout llSpecitemHome;
    @BindView(R.id.mylv_fir_home)
    MyListView mylvFirHome;
    @BindView(R.id.iv_icon_location_top)
    ImageView ivIconLocationTop;
    @BindView(R.id.tv_item_nearmarket_top)
    TextView tvItemNearmarketTop;
    @BindView(R.id.it)
    TextView it;
    @BindView(R.id.mylv_two)
    MyListView mylvTwo;
    @BindView(R.id.ll_sv_home)
    LinearLayout llSvHome;
    @BindView(R.id.ptr_sv)
    PullToRefreshScrollView ptrSv;
    private List<String> vpStringList;
    private RadioButton radiotwo;
    private HomeContract.Presenter presenter;
    private View header;
    private Mainbean mainbean;
    private String urlString = UrlConfig.Path.HOME_MAIN;
    private Map<String, String> urlmap = new ArrayMap<>();

    @Override
    public void initViews() {
        radiotwo = (RadioButton) getActivity().findViewById(R.id.rb_shop);
        presenter = new HomePresenter(this);


    }

    @Override
    public void initData() {


    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initMyData();
        urlmap.put(UrlConfig.Params.MARKET_ID, UrlConfig.DefaultVaule.MARKET_ID + "");
        presenter.getMainBean(urlString, urlmap);
    }

    private void initMyData() {
        vpStringList = new ArrayList<>();
        vpStringList.add("http://image.zenmechi.cc/img/1/cf/51/1cf5157ccde86bfe08.png");
        vpStringList.add("http://image.zenmechi.cc/img/9/5f/5f/95f5f57ccda8c09d07.png");
        vpStringList.add("http://image.zenmechi.cc/img/e/b2/07/eb20757ccdab67b14e.jpg");
        vpHome.setFocusable(true);
        //网络地址
        vpHome.setViewUrls(vpStringList);
        //本地资源
//        banner.setViewRes(viewRes);
        //添加点击监听
        vpHome.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                radiotwo.setChecked(true);
            }
        });
    }

    @Override
    public void onSuccess(Mainbean mainbean) {
        this.mainbean = mainbean;
        if (mainbean != null && mainbean.getResult() != null) {
            if (mainbean.getResult().getGoodsSpecialsList()!=
                    null&&mainbean.getResult().getGoodsSpecialsList().size()!=0)
            {

                    if (mainbean.getResult().getGoodsSpecialsList().get(0)!=null)
                    presenter.lodpic(ivOnespcHome,
                            getActivity(),mainbean.getResult().getGoodsSpecialsList().get(0).getPic());
                    if (mainbean.getResult().getGoodsSpecialsList().get(1)!=null)
                    presenter.lodpic(ivTwospcHome,
                            getActivity(),mainbean.getResult().getGoodsSpecialsList().get(1).getPic());
                    if (mainbean.getResult().getGoodsSpecialsList().get(2)!=null)
                    presenter.lodpic(ivThiredspcHome,
                            getActivity(),mainbean.getResult().getGoodsSpecialsList().get(2).getPic());

            }
            if (mainbean.getResult().getGoodsHotList() != null
                    && mainbean.getResult().getGoodsHotList().size() != 0
                    ) {
                presenter.setListViewAdapter(mylvFirHome, getActivity(), mainbean.getResult().getGoodsHotList());
                setfirlvlisten(mylvFirHome,mainbean.getResult().getGoodsHotList());
            }
            if (mainbean.getResult().getNearbyMerchantList() != null
                    && mainbean.getResult().getNearbyMerchantList().size() != 0
                    ) {
                presenter.setListViewAdaptertwo(mylvTwo, getActivity(), mainbean.getResult().getNearbyMerchantList());
            }
        }
        ptrSv.onRefreshComplete();
        setptrSv();
    }

    private void setfirlvlisten(MyListView mylvFirHome, final List<GoodsHotListBean> goodsHotList) {
        mylvFirHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startGoodsDetailActivity(goodsHotList.get(i).getGoods_id());
            }
        });
    }
    @Override
    public void startGoodsDetailActivity(int goods_id) {
        Intent intent=new Intent(getActivity(), DetailActivity.class);
        intent.putExtra("id",goods_id+"");
        startActivity(intent);
    }


    @Override
    public void setptrSv() {
        ptrSv.setMode(PullToRefreshBase.Mode.BOTH);
        ptrSv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                urlmap.put(UrlConfig.Params.MARKET_ID, UrlConfig.DefaultVaule.MARKET_ID + "");
                presenter.getMainBean(urlString, urlmap);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                urlmap.put(UrlConfig.Params.MARKET_ID, UrlConfig.DefaultVaule.MARKET_ID + "");
                presenter.getMainBean(urlString, urlmap);
            }
        });
    }


    @Override
    public void onFail(Exception e) {
        Toast.makeText(getActivity(), "加载数据失败", Toast.LENGTH_SHORT).show();
        Log.i("fail", "homeloading data fail");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.tv_location_hone, R.id.iv_search_home, R.id.rela_activity_home, R.id.rela_cook_home, R.id.rela_order_home, R.id.rela_sign_home, R.id.tv_more_home})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_location_hone:
                Intent intent=new Intent(getActivity(), LocationActiviy.class);
                startActivity(intent);
                break;
            case R.id.iv_search_home:
                break;
            case R.id.rela_activity_home:
                break;
            case R.id.rela_cook_home:
                break;
            case R.id.rela_order_home:
                break;
            case R.id.rela_sign_home:
                break;
            case R.id.tv_more_home:
                startActivity(new Intent(getActivity(),SpecialActivity.class));
                break;
        }
    }
}
